/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import model.Aluno;
import model.Lista;
import model.ListaAluno;

/**
 *
 * @author edilson
 */
public class ListaAlunoJpaController {

    public ListaAlunoJpaController() {
        emf = DaoManager.getEmf();
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ListaAluno listaAluno) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(listaAluno);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ListaAluno listaAluno) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            listaAluno = em.merge(listaAluno);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = listaAluno.getId();
                if (findListaAluno(id) == null) {
                    throw new NonexistentEntityException("The listaAluno with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ListaAluno listaAluno;
            try {
                listaAluno = em.getReference(ListaAluno.class, id);
                listaAluno.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The listaAluno with id " + id + " no longer exists.", enfe);
            }
            em.remove(listaAluno);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ListaAluno> findListaAlunoEntities() {
        return findListaAlunoEntities(true, -1, -1);
    }

    public List<ListaAluno> findListaAlunoEntities(int maxResults, int firstResult) {
        return findListaAlunoEntities(false, maxResults, firstResult);
    }

    private List<ListaAluno> findListaAlunoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from ListaAluno as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ListaAluno findListaAluno(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ListaAluno.class, id);
        } finally {
            em.close();
        }
    }

    public int getListaAlunoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from ListaAluno as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public List<ListaAluno> findListaAluno(Lista lista) {

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT p FROM ListaAluno p WHERE p.lista = :lista");
            q.setParameter("lista", lista);
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public List<ListaAluno> findListaAluno(Aluno aluno) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT p FROM ListaAluno p WHERE p.aluno = :aluno");
            q.setParameter("aluno", aluno);
            return q.getResultList();
        } finally {
            em.close();
        }
    }
}
